package qa.tools.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import qa.tools.mapper.AutoResultMapper;
import qa.tools.model.po.AutoResult;
import qa.tools.model.vo.AutoResultVo;
import qa.tools.service.AutoResultService;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiJiang
 * @since 2021-09-08
 */
@Service
public class AutoResultServiceImpl extends ServiceImpl<AutoResultMapper, AutoResult> implements AutoResultService {

    @Autowired
    private AutoResultMapper AutoResultMapper;

    @Override
    public Map<String, Object> queryApiResult(AutoResultVo AutoResultVo) {

        String serviceName = AutoResultVo.getServiceName();
        Integer runType = AutoResultVo.getRunType();
        Integer testSet = AutoResultVo.getTestSet();

        Date beginTime = AutoResultVo.getBeginTime();
        Date endTime = AutoResultVo.getEndTime();


        Page<AutoResult> page = new Page<>(AutoResultVo.getPageNum(), AutoResultVo.getPageSize());
        QueryWrapper<AutoResult> queryWrapper = new QueryWrapper<>();

        if (StringUtils.hasLength(serviceName)) {
            queryWrapper.eq("service_name", serviceName);
        }
        if (StringUtils.hasLength(runType.toString())) {
            queryWrapper.eq("run_type", runType);
        }
        if (StringUtils.hasLength(testSet.toString())) {
            queryWrapper.eq("test_set", testSet);
        }
        if (StringUtils.hasLength(beginTime.toString()) && StringUtils.hasLength(endTime.toString())) {
            queryWrapper.between("run_time", beginTime, endTime);
        }

        queryWrapper.orderByDesc("id");
        Page<AutoResult> page1 = AutoResultMapper.selectPage(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("total", page1.getTotal());
        map.put("size", page1.getSize());
        map.put("pages", page1.getPages());
        map.put("current", page1.getCurrent());
        map.put("list", page1.getRecords());

        return map;
    }
    @Override
    public Map<String, Object> queryAll(Long current, Long size) {

        Page<AutoResult> page = new Page<>(current, size);

        Page<AutoResult> page1 = AutoResultMapper.selectPage(page, null);

        Map<String, Object> map = new HashMap<>();
        map.put("total", page1.getTotal());
        map.put("size", page1.getSize());
        map.put("pages", page1.getPages());
        map.put("current", page1.getCurrent());
        map.put("list", page1.getRecords());

        return map;
    }


}
